package ru.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class TelerikPage extends AnyPage1 {

	public TelerikPage(PageManager1 pages) {
		super(pages);
	}

	public TelerikPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By
				.xpath("//label")));
		return this;
	}

	@FindBy(xpath = "//div[4]//tr[2]//li[2]/a/span/span[2]")
	public WebElement calendarFolder;

	@FindBy(xpath = "//div[4]//tr[2]//li[3]/a/span/span[2]")
	public WebElement contactsFolder;

	@FindBy(xpath = "//div[4]//tr[2]//li[4]/a/span/span[2]")
	public WebElement notesFolder;

	@FindBy(xpath = "//div[4]//tr[3]//li[1]/a/span/span[2]")
	public WebElement mailFolder;

	public TelerikPage clickCalendarFolder() {
		calendarFolder.click();
		return pages.telerikPage;
	}

	public TelerikPage clickContactsFolder() {
		contactsFolder.click();
		return pages.telerikPage;
	}

	public TelerikPage clickNotesFolder() {
		notesFolder.click();
		return pages.telerikPage;
	}

	public TelerikPage clickMailFolder() {
		mailFolder.click();
		return pages.telerikPage;
	}
  
        /*
         * Click all mail records in each Sub-field of the Mail's Input folder 
         */
	public TelerikPage clickMailInputFolders() throws Exception {
		int n = 1; // Sub-field number
		int i = 0; // record number
		int count = 0; // total number of records in each sub-field
		while (n < 6) {
			i = 0;
			wait.until(
				presenceOfElementLocated(By
					.xpath("//div[4]//tr[2]//td[1]//tr[1]//div[3]//li[3]//li["
						+ n + "]//span[2]"))).click();
			count = driver
				.findElements(
					By.xpath("//tr[contains(@id, 'ctl00_ContentPlaceHolder2_RadGrid1_ctl00')]"))
					    .size();
			if (count > 50) { // just to be reasonable how far to go
				count = 50;
			}

			while (i < count) {
				wait.until(
					presenceOfElementLocated(By
						.xpath("//tr[@id='ctl00_ContentPlaceHolder2_RadGrid1_ctl00__"
							+ i + "']/td[2]"))).click();
				i++;
			}
			n++;
		}
		return pages.telerikPage;
	}

}

